package com.springdoor.wx.filter;

import com.springdoor.wx.core.domain.entity.LoginUser;
import com.springdoor.wx.core.token.TokenService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class RefreshTokenRequestInterceptor implements HandlerInterceptor {

    @Resource
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser(request);

        if (loginUser == null || loginUser.getUser() == null) {
            return true;
        }
        tokenService.refreshToken(loginUser);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
